package chances.wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import chances.wechat.entity.WechatUser;
import chances.wechat.listener.LoginListener;
import chances.wechat.model.WechatToken;

@RestController
@RequestMapping(path="/wechat")
public class WechatController extends UserAbstractController {

//	@Autowired
//	TokenFactory tokenFactory;
	
	@Autowired
	LoginListener loginListener;
	
	public static final String OAUTH_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
	
	public static final String responseType = "code";
	
	@RequestMapping("/snslogin")
	public WechatUser sns(@RequestParam("code") String code,
			@RequestParam("type")  String type,
			@RequestParam("state")  String state,
			@RequestParam("app_code") String appCode) {
		
//		TokenSet tokenSet = tokenFactory.getTokenSet(appCode);
//		OauthAPI oauthAPI = new OauthAPI(tokenSet.getAppId(), tokenSet.getSecret());
//		String atoken = tokenFactory.getAccessToken(appCode);
//		oauthAPI.setAccessToken(atoken);
//		Map<String, Object> accessTokenResponse = oauthAPI.getSnsToken(code);
//		
//		if(!accessTokenResponse.containsKey("errcode")){
//			String token = (String) accessTokenResponse.get("access_token");
//			String openId = (String) accessTokenResponse.get("openid");
//			logLogin(tokenSet.getAppId(), openId, type, appCode, state);
//			WechatUser wechatUser = loginListener.getWechatUser(appCode, openId, type, token);			
//			if("code".equals(type)){
//				super.deviceBind(wechatUser, state);
//			}
//			return wechatUser;
//		}else{
//			return new WechatUser();
//		}
	    return null;
		
		
	}

	
	
	@RequestMapping("/token")
	public WechatToken getToken(@RequestParam("app_code") String appCode) {
//		TokenSet tokenSet = tokenFactory.getTokenSet(appCode);
//		WechatToken wechatToken = new WechatToken();
//		wechatToken.setAppId(tokenSet.getAppId());
//		wechatToken.setToken(tokenSet.getAccessToken());
//		wechatToken.setJsToken(tokenSet.getJsApiTicket());
//		wechatToken.setEffectTime(tokenSet.getAccessTokenStartTime() + 
//				System.currentTimeMillis());
//		return wechatToken;
	    return null;
	}
	
}
